package com.financing.platform.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.CompanyDTO;
import com.financing.platform.service.ICompanyService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;
import com.financing.platform.util.SecureUtil;

@Controller
public class CompanyAction extends BaseAction{
	
	@Autowired
	ICompanyService companyServiceImpl;
	
	@RequestMapping("/queryCompany.shtml")
	public ModelAndView queryCompany(HttpServletRequest request,
			HttpServletResponse response,CompanyDTO companyDTO) {
			log.info("Begin query company info !");
			request.getSession().setAttribute(Dict.requestID, "queryCompany.shtml");
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			List<CompanyDTO> companyList = companyServiceImpl.listCompany(companyDTO);
			
			resultMap.put("companyList", companyList);
			
			log.info("End query company info !");
			return new ModelAndView("companyManager",resultMap);
	}
	
	@RequestMapping("/updateCompany.shtml")
	public ModelAndView updateCompany(HttpServletRequest request,
			HttpServletResponse response,CompanyDTO companyDTO) {
			log.info("Begin update company info !");
			
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			DTOdateSet.setUpdateInfo(companyDTO, request.getSession());
			
			try {
				resultMap.put("result", companyServiceImpl.updateCompany(companyDTO));
			} catch (SQLException e) {
				log.error("Update company status error ",e);
				resultMap.put("result", 0);
			}
			List<CompanyDTO> companyList = companyServiceImpl.listCompany(companyDTO);
			resultMap.put("companyList", companyList);
			
			log.info("End update company info !");
			return new ModelAndView("CompanyManage",resultMap);
	}
	
	@RequestMapping("/addCompany.shtml")
	public ModelAndView addCompany(HttpServletRequest request,
			HttpServletResponse response,CompanyDTO companyDTO) {
			log.info("Begin add company info !");
			
			companyDTO.setuLoginName(companyDTO.getuEmail());
			companyDTO.setuName(companyDTO.getuCompany());
			
			companyDTO.setuPassword(SecureUtil.MD5EncryptAndBase64(companyDTO.getuPassword()));
			
			DTOdateSet.setCreateInfo(companyDTO, request.getSession());
			DTOdateSet.setUpdateInfo(companyDTO, request.getSession());
			
			Map<String,Object> resultMap = new HashMap<String,Object>();
			
			try {
				companyServiceImpl.addCompany(companyDTO);
				resultMap.put(Dict.result, Dict.result_success );
			} catch (SQLException e) {
				log.error("ADD company status error ",e);
				resultMap.put(Dict.result, Dict.result_error);
			}
			List<CompanyDTO> companyList = companyServiceImpl.listCompany(companyDTO);
			resultMap.put("companyList", companyList);
			
			log.info("End add company info !");
			return new ModelAndView("CompanyManage",resultMap);
	}
}
